package com.annual.common.utils;

import org.springframework.util.DigestUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;

public class MyStringUtils {
    /**
     * 生成12位的随机数
     */

    public static Long number12() {
        long number12 = (long) (Math.random() * (999999999999L - 100000000000L)) + 100000000000L;
        return number12;
    }

    public static String toSqlString(String str) {
        String line = "";
        char[] arr = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 'A' && arr[i] <= 'Z') {
                line += "_" + (char) (arr[i] + 32);
            } else {
                line += arr[i];
            }
        }
        return line;

    }

    /**
     * 对多选选项进行排序 如 ZDAB -> ZABD, 如果不是以Z开头，原样返回
     *
     * @param checkboxOptionsStr
     * @return
     */
    public static String sortCheckboxOptions(String checkboxOptionsStr) {
        if (!checkboxOptionsStr.startsWith("Z")) return checkboxOptionsStr;
        char[] str;
        char temp;
        String string = checkboxOptionsStr.substring(1);
        str = string.toCharArray();
        for (int i = 0; i < string.length(); i++) {
            for (int j = string.length() - 1; j > i; j--) {
                if (str[j] <= str[j - 1]) {
                    temp = str[j];
                    str[j] = str[j - 1];
                    str[j - 1] = temp;
                }
            }
        }
        return "Z" + new String(str);
    }

    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 生成uuid
     *
     * @return
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }

    /**
     * 判断字符是否为空，和是否在对定长度内
     *
     * @param str    字符�?
     * @param minLen �?小长�?
     * @param maxLen �?大长�?
     * @return true表示合法，false表示不合�?
     */
    public static boolean checkLen(String str, int minLen, int maxLen) {
        boolean isOk = true;
        if (str == null || str.length() < minLen || str.length() > maxLen) {
            isOk = false;
        }
        return isOk;
    }

    /**
     * 生成加密�?
     *
     * @param str 原始字符�?
     * @return 加密字符�?
     */
    public static String getEncodeStr(String str) {
        String encodeStr = str;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            encodeStr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    /**
     * 两次Md5加密
     *
     * @param userPass
     * @param pwdkey
     * @return
     */
    public static String twiceMd5Encode(String userPass, String pwdkey) {
        String str = "" + pwdkey + userPass;
        try {
            str = DigestUtils.md5DigestAsHex(DigestUtils.md5DigestAsHex(str.getBytes("utf-8")).getBytes("utf-8"));
            str = "###" + str;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 判断是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 判断是否不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }


    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static Integer getTimeMillis() {
        Long timeMillis = System.currentTimeMillis();
        return timeMillis.intValue();
    }

    /**
     * 压缩
     * @param text
     * @return
     */
    public static byte[] zipBase64(String text){
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)){
                deflaterOutputStream.write(text.getBytes(Charset.defaultCharset()));
            }
            return out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[]{};
    }

    /**
     * 解押字符串
     * @param zipedStr
     * @return
     */
    public static byte[] unZipBase64(String zipedStr){
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()){
            try (InflaterOutputStream outputStream = new InflaterOutputStream(os)){
                outputStream.write(new BASE64Decoder().decodeBufferToByteBuffer(zipedStr).array());
            }
            return os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[]{};
    }


    public static void main(String[] args) {
        System.out.println(MyStringUtils.getEncodeStr("123456"));
    }
}
